﻿using System;
using System.Threading;

// 定义闹钟事件参数
public class AlarmEventArgs : EventArgs
{
    public string Message { get; set; }
    public DateTime Time { get; set; }
}

// 闹钟类
public class AlarmClock
{
    // 定义事件
    public event EventHandler<AlarmEventArgs> Tick;
    public event EventHandler<AlarmEventArgs> Alarm;

    private DateTime _alarmTime;
    private bool _isRunning;

    public AlarmClock(DateTime alarmTime)
    {
        _alarmTime = alarmTime;
    }

    // 启动闹钟
    public void Start()
    {
        _isRunning = true;
        Console.WriteLine("闹钟已启动...");

        while (_isRunning)
        {
            // 获取当前时间
            DateTime now = DateTime.Now;

            // 触发Tick事件
            OnTick(now);

            // 检查是否到达闹铃时间
            if (now.Hour == _alarmTime.Hour &&
                now.Minute == _alarmTime.Minute &&
                now.Second == _alarmTime.Second)
            {
                // 触发Alarm事件
                OnAlarm(now);
                _isRunning = false; // 响铃后停止
            }

            // 等待1秒
            Thread.Sleep(1000);
        }
    }

    // 停止闹钟
    public void Stop()
    {
        _isRunning = false;
    }

    // 触发Tick事件
    protected virtual void OnTick(DateTime time)
    {
        Tick?.Invoke(this, new AlarmEventArgs
        {
            Message = "嘀嗒...",
            Time = time
        });
    }

    // 触发Alarm事件
    protected virtual void OnAlarm(DateTime time)
    {
        Alarm?.Invoke(this, new AlarmEventArgs
        {
            Message = "叮铃铃!!! 时间到了!!!",
            Time = time
        });
    }
}

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("请输入闹钟时间（格式：HH:mm:ss）:");
        string input = Console.ReadLine();

        if (DateTime.TryParse(input, out DateTime alarmTime))
        {
            // 创建闹钟实例
            var alarmClock = new AlarmClock(alarmTime);

            // 订阅事件
            alarmClock.Tick += (sender, e) => {
                Console.WriteLine($"{e.Time.ToLongTimeString()} - {e.Message}");
            };

            alarmClock.Alarm += (sender, e) => {
                Console.WriteLine($"{e.Time.ToLongTimeString()} - {e.Message}");
                Console.Beep(); // 发出提示音
            };

            // 启动闹钟
            alarmClock.Start();
        }
        else
        {
            Console.WriteLine("时间格式不正确！");
        }

        Console.WriteLine("按任意键退出...");
        Console.ReadKey();
    }
}